package cn.foolishbird.crow.core;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.Objects;

/**
 * @author foolish-bird
 */
public class DefaultStrategyManager<S> implements StrategyManager<S> {

    private Logger logger = LoggerFactory.getLogger(DefaultStrategyManager.class);

    private final S main;

    private Map<Enum<? extends CrowEnum>, S> strategyMap;

    public DefaultStrategyManager(S main, Map<Enum<? extends CrowEnum>, S> strategyMap) {
        this.main = main;
        this.strategyMap = strategyMap;
    }

    @Override
    public S defaultStrategy() {
        if (Objects.isNull(main)) {
            throw new IllegalArgumentException("main is null");
        }
        return main;
    }

    @Override
    public S strategy(Enum<? extends CrowEnum> type) {
        if (Objects.isNull(type)) {
            throw new IllegalArgumentException("type is null");
        }
        if (Objects.isNull(strategyMap) || strategyMap.isEmpty()) {
            throw new IllegalArgumentException("strategyMap is null");
        }

        S strategy = strategyMap.get(type);
        if (Objects.isNull(strategy)) {
            CrowEnum crow = (CrowEnum) type;
            throw new IllegalArgumentException(crow.content() + " is not currently supported");
        }
        return strategy;
    }
}
